// 获取单个app
const { pick } = require('lodash');
const dayjs = require('dayjs');
const { apps } = require('../model/index');
const commonData = require('../lib/commonData').base;

module.exports = function () {
    this.get('/tvapi/v1/store/apkinfo', async (ctx) => {
        console.log(ctx.request.href);
        const package = ctx.request.query.apkid;
        if (/^my\.app\./.test(package)) {
            ctx.redirect('https://baidu.com');
        }
        const app = await apps.findOne({ package });
        ctx.type = 'text/html;charset=UTF-8';

        const data = Object.assign({
            description: app.name,
            // path: 'http://app.tpvstore.com/' + app.apkPath,
            path: app.dlLink,
        }, commonData, app.toObject());
        data.updateTime = dayjs(data.updateTime).format('YYYY-MM-DD HH:mm:ss');
        ctx.response.body = JSON.stringify(pick(data, ["author", "categories", "description", "deviceSupport", "dwnCount", "icon", "id", "md5hash", "name", "package", "path", "rating", "size", "sshots", "supportPhone", "supportTv", "updateTime", "verNote", "vercode", "vername"]));
    });
}
